package cn.kys.common.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.kys.common.exception.BadRequestException;
import org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


/**功能描述：验证工具
 * @Auther:http://www.kys.cn
 * @Date:2020/3/23
 * @Description:cn.kys.common.utils
 * @version:1.0
 */
public class ValidationUtil {
    /**
     * 验证空
     */
    public static void isNull(Object obj, String entity, String parameter , Object value){
        if(ObjectUtil.isNull(obj)){
            String msg = entity + " 不存在: "+ parameter +" is "+ value;
            throw new BadRequestException(msg);
        }
    }

    /**
     * 验证是否为邮箱
     */
    public static boolean isEmail(String email) {
        return new EmailValidator().isValid(email, null);
    }

    public static boolean compareToCurrentTime(String dateStr){
        boolean expired = true;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");//创建日期转换对象HH:mm:ss为时分秒，年月日为yyyy-MM-dd
        try {
            Date date = df.parse(dateStr);//将字符串转换为date类型

            //当前时间
            long currentTime = System.currentTimeMillis();
            long argTime = date.getTime();
            if(currentTime > argTime){
                expired = true;
            }else{
                expired = false;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return expired;
    }
}
